import request from './request'

/**
 * 工具执行步骤接口定义
 */
export interface ToolStep {
  type: 'thought' | 'tool_call';
  tool_name?: string;
  tool_label?: string;
  status: 'running' | 'completed' | 'failed';
  content?: string;
  input?: any;
  output?: string;
  images?: string[];
  execution_time?: number;
}

/**
 * 对话消息接口定义
 */
export interface ConversationMessage {
  id: number;
  conversation_id: number;
  role: 'user' | 'assistant' | 'system' | 'tool';
  content: string;
  created_at: string;
  steps?: ToolStep[];
  tool_calls?: any[];
  tool_results?: any;
  meta_data?: any;
}

/**
 * 对话接口定义
 */
export interface Conversation {
  id: number;
  title: string;
  user_id: number;
  created_at: string;
  updated_at: string;
  status: 'in_progress' | 'completed' | 'failed';
  mode: 'chat' | 'task_runner';
}

/**
 * 创建对话
 * @param title 对话标题
 * @param content 初始消息内容
 * @returns 创建的对话
 */
export async function createConversation(title: string, content: string): Promise<Conversation> {
  try {
    console.log('createConversation called with title:', title, 'and content:', content)
    const response = await request.post('/v1/conversations', {
      title: title,
      content: content
    })
    // 后端返回格式：{ code: 200, msg: "", data: { ...conversation } }
    // request拦截器已经处理为直接返回data字段的内容
    console.log('createConversation response:', response)
    console.log('createConversation response type:', typeof response)
    console.log('createConversation response has id:', 'id' in response)
    return response as unknown as Conversation
  } catch (error) {
    console.error('创建对话失败:', error)
    throw error
  }
}

/**
 * 获取对话详情
 * @param conversationId 对话ID
 * @returns 对话详情
 */
export async function getConversation(conversationId: number): Promise<Conversation> {
  try {
    const response = await request.get(`/v1/conversations/${conversationId}`)
    // 后端返回格式：{ code: 200, msg: "", data: { ...conversation } }
    // request拦截器已经处理为直接返回data字段的内容
    return response as unknown as Conversation
  } catch (error) {
    console.error('获取对话详情失败:', error)
    throw error
  }
}

/**
 * 获取对话消息列表
 * @param conversationId 对话ID
 * @returns 对话消息列表
 */
export async function getConversationMessages(conversationId: number): Promise<ConversationMessage[]> {
  try {
    const response = await request.get(`/v1/conversations/${conversationId}/messages`)
    // 后端返回格式：{ code: 200, msg: "", data: [...messages] }
    // request拦截器已经处理为直接返回data字段的内容
    return response as unknown as ConversationMessage[]
  } catch (error) {
    console.error('获取对话消息失败:', error)
    throw error
  }
}

/**
 * 发送消息
 * @param conversationId 对话ID
 * @param content 消息内容
 * @returns 发送的消息和AI回复
 */
export async function sendMessage(conversationId: number, content: string): Promise<any> {
  try {
    const response = await request.post(`/v1/conversations/${conversationId}/messages`, {
      content: content
    })
    // 后端返回格式：{ code: 200, msg: "", data: { message: { content: "..." }, tool_execution_logs: [...] } }
    // request拦截器已经处理为直接返回data字段的内容
    return response
  } catch (error) {
    console.error('发送消息失败:', error)
    throw error
  }
}

/**
 * 获取用户对话列表
 * @returns 对话列表
 */
export async function getUserConversations(): Promise<Conversation[]> {
  try {
    const response = await request.get('/v1/conversations')
    // 后端返回格式：{ code: 200, msg: "", data: [...conversations] }
    // request拦截器已经处理为直接返回data字段的内容
    return response as unknown as Conversation[]
  } catch (error) {
    console.error('获取对话列表失败:', error)
    throw error
  }
}
